<!DOCTYPE HTML>

<html>
<head>
<title>Worker requestAnimationFrame after GPU crash</title>
<style type="text/css">
.nomargin {
  margin: 0px auto;
}
</style>
<script id="worker" type="text/worker">
// This test kills the GPU process, which takes some time to recover,
// so give it a few seconds before proceeding.
var numFramesBeforeEnd = 200;

function frame() {
  if (--numFramesBeforeEnd == 0) {
    postMessage("DONE");
  } else {
    requestAnimationFrame(frame);
  }
}
frame();
postMessage("READY");
</script>
<script>
var numFramesBeforeFail = 600;

function sendResult(status, detail) {
  if (detail)
    console.log(detail);
  if (window.domAutomationController) {
    window.domAutomationController.send(status);
  } else {
    console.log(status);
  }
}

function main() {
  var blob = new Blob([document.getElementById('worker').textContent]);
  var worker = new Worker(URL.createObjectURL(blob));
  var done = false;

  worker.addEventListener("message", (ev) => {
    if (ev.data == "READY") {
      // Sending LOADED will make the GPU crash.
      sendResult("LOADED");
      frame();
    } else if (ev.data == "DONE") {
      sendResult("SUCCESS", "Test complete");
      done = true;
    } else {
      sendResult("FAILURE", "Unexpected reply from worker: " + ev.data);
    }
  });

  function frame() {
    if (done) return;
    if (--numFramesBeforeFail == 0) {
      sendResult("FAILURE", "Worker.requestAnimationFrame didn't recover");
    } else {
      requestAnimationFrame(frame);
    }
  }
}
</script>
</head>
<body onload="main()">
<canvas id="c" width="300" height="300" class="nomargin" style="position:absolute; top:0px; left:0px;"></canvas>
</div>
</body>
</html>
